package com.link2room.soundwave.util;

import com.greencloud.api.ApiClient;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.SysoptionUtil;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.constant.GcPmsConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sss on 2017/4/6.
 */
public class GcPmsUtil {
    public static Map<String,ApiClient> gcClientMap=new HashMap<>();

    public static void setGcClient(String lhotelGroupCode) throws Exception {
        String gcURL = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.URL);
        String gcMemberURL = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.MEMBERURL);
        String gcAPPKEY = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.APPKEY);
        String gcSECRET = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.SECRET);
        String gcHotelGroupCode = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.GCHOTELGROUPCODE);
        String gcUsercode = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.USERCODE);
        String gcPassword = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.PASSWORD);
        if(gcClientMap.get(lhotelGroupCode)==null){
            ApiClient apiClient=new ApiClient(gcURL, gcAPPKEY, gcSECRET, gcHotelGroupCode, gcUsercode, gcPassword) {
                // 日志
                @Override
                protected void log(String requestUrl, Map<String, String> params, String response, Throwable throwable) {
                    System.out.println(response);
                }

                // 更换JSON反序列化工具
                @Override
                protected <T> T fromJSONString(String text, Class<T> clazz) {
                    return super.fromJSONString(text, clazz);
                }
            };
            // 非必需，使用到会员接口需设置会员地址
            apiClient.setMemberUrl(gcMemberURL);
            gcClientMap.put(lhotelGroupCode,apiClient);
        }
    }

    public static ApiClient getGcClient(String lhotelGroupCode) throws Exception {
        String gcURL = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.URL);
        String gcMemberURL = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.MEMBERURL);
        String gcAPPKEY = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.APPKEY);
        String gcSECRET = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.SECRET);
        String gcHotelGroupCode = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.GCHOTELGROUPCODE);
        String gcUsercode = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.USERCODE);
        String gcPassword = SysoptionUtil.getSysOption(lhotelGroupCode, GcPmsConstant.CONFIG_CATEGORY,GcPmsConstant.CONFIG_ITEM.PASSWORD);
        ApiClient apiClient=gcClientMap.get(lhotelGroupCode);
        if(apiClient==null){
            apiClient=new ApiClient(gcURL, gcAPPKEY, gcSECRET, gcHotelGroupCode, gcUsercode, gcPassword) {
                // 日志
                @Override
                protected void log(String requestUrl, Map<String, String> params, String response, Throwable throwable) {
                    System.out.println(response);
                }

                // 更换JSON反序列化工具
                @Override
                protected <T> T fromJSONString(String text, Class<T> clazz) {
                    return JacksonUtil.jsonToBean(text, clazz);
                }
            };
            if(apiClient==null){
                throw new SoundwaveException(SoundwaveSysExceptionEnum.NO_GCCLIENT,"lhotelGroupCode:"+lhotelGroupCode+
                        "|gcURL:"+gcURL+"|gcAPPKEY:"+gcAPPKEY+"|gcSECRET:"+gcSECRET+"|gcHotelGroupCode:"+gcHotelGroupCode
                        +"|gcUsercode:"+gcUsercode+"|gcPassword:"+gcPassword);
            }
            // 非必需，使用到会员接口需设置会员地址
            apiClient.setMemberUrl(gcMemberURL);

            gcClientMap.put(lhotelGroupCode,apiClient);
        }
        return apiClient;
    }

}
